package saleMarket.controller.basic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import saleMarket.domain.ShopOwner;
import saleMarket.service.ShopOwnerService;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
@Slf4j
@WebServlet("/shopOwner.ctl")
public class ShopOwnerController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //根据request对象，获得代表参数的JSON字串
        String student_json = JSONUtil.getJSON(request);
        //将JSON字串解析为ShopOwner对象
        ShopOwner showOwnerToAdd = JSON.parseObject(student_json, ShopOwner.class);
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //在数据库表中增加ShopOwner对象
        try {
            if (ShopOwnerService.getInstance().add(showOwnerToAdd)){
                message.put("message", "增加成功");
            }else {
                message.put("message", "增加失败");
            }
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        } catch (Exception e) {
            message.put("message", "网络异常");
            e.printStackTrace();
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        }
        //响应message到前端
        response.getWriter().println(message);
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Object id_obj = request.getParameter("id");
        int id_int = Integer.parseInt(id_obj.toString());
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            //到数据库表中删除对应的ShopOwner
            if (ShopOwnerService.getInstance().delete(id_int)){
                respMessage_jsonObj.put("message", "删除成功");
            }else {
                respMessage_jsonObj.put("message", "删除失败");
            }
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        }
        //响应respMessage_jsonObj到前端
        response.getWriter().println(respMessage_jsonObj);
    }

    @Override
    protected void doPut(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException{
        //根据request对象，获得代表参数的JSON字串
        String shopOwner_json = JSONUtil.getJSON(request);
        //将JSON字串解析为ShopOwner对象
        ShopOwner shopOwnerToUpdate = JSON.parseObject(shopOwner_json, ShopOwner.class);
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //到数据库表修改ShopOwner对象对应的记录
        try {
            if (ShopOwnerService.getInstance().update(shopOwnerToUpdate)) {
                message.put("message", "修改成功");
            }else {
                message.put("message", "修改失败");
            }
        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        } catch (Exception e) {
            message.put("message", "网络异常");
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        }
        //响应message到前端
        response.getWriter().println(message);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        Object id_obj = request.getParameter("id");
        System.out.println(id_obj);
        try {
            //如果id_Obj != null, 说明请求数据有id键，是请求某个ShopOwner对象
            if (id_obj != null) {
                int id_int = Integer.parseInt(id_obj.toString());
                System.out.println(id_int);
                responseShopOwner(id_int, response);
            } else {
                responseShopOwners(response);
            }
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            e.printStackTrace();
            //响应respMessage_jsonObj到前端
            response.getWriter().println(respMessage_jsonObj);
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //响应respMessage_jsonObj到前端
            response.getWriter().println(respMessage_jsonObj);
            //打印异常栈，方便调试
            log.error(e.getMessage(),e);
        }
    }

    //响应一个ShopOwner对象
    private void responseShopOwner(int id, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        Collection<ShopOwner> shopOwners = new ArrayList<>();
        //根据id查找ShopOwner
        ShopOwner shopOwner = ShopOwnerService.getInstance().find(id);
        //第二个参数可以防止循环引用
        String shopOwners_jsonStr = JSON.toJSONString(shopOwners,
                SerializerFeature.DisableCircularReferenceDetect);

        //响应ShopOwner_json到前端
        response.getWriter().println(shopOwners_jsonStr);
    }

    //按分页信息响应符合条件的ShopOwner对象和对象总数
    private void responseShopOwners(HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //分页获得满足条件的所有ShopOwner
        Collection<ShopOwner> shopOwners = ShopOwnerService.getInstance()
                .findAll();
        //第二个参数可以防止循环引用
        String shopOwners_jsonStr = JSON.toJSONString(shopOwners,
                SerializerFeature.DisableCircularReferenceDetect);
        //响应resp_jsonObj到前端
        response.getWriter().println(shopOwners_jsonStr);
    }

}
